/*
 * Copyright (c) 2020, OneOS Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-11-13     OneOs Team     the first version
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <os_kernel.h>
#include <oneos_config.h>
#include <os_dbg_ext.h>
#include "opencc_general.h"
#include "opencc_intf.h"

/* Test function */
static int cc_test(int argc, char **argv)
{
    opencc_info_t src = {NULL};
    opencc_info_t *dst = NULL; 
    CC_COMMON_CHARACTER_TYPE_E dst_type = CC_CHARC_TYPE_UTF8;

    os_kprintf("Character conversion tesd cmd 'help' information: \n"
                "Usage: cc_test <text> [source encoding] [dst encoding]\n"
                "[xxx encoding]: [0]:Utf-8, [1]:GBK \n");
    
    if (argc < 2 || argc > 4)
    {
        os_kprintf("Character conversion tesd cmd input wrong paramers.\n");
        return -1;
    }

    /* create the input paramers */
    src.buf = (void *)argv[1];
    src.buf_size = strlen(argv[1]);
    /* default conversion between 2 character sets is GBK TO UTF-8 */
    src.char_type = CC_CHARC_TYPE_GBK;
    if (argc == 3)
    {
        src.char_type = (CC_COMMON_CHARACTER_TYPE_E)atoi(argv[2]);
    }
    else if (argc == 4)
    {
        src.char_type = (CC_COMMON_CHARACTER_TYPE_E)atoi(argv[2]);
        dst_type = (CC_COMMON_CHARACTER_TYPE_E)atoi(argv[3]);
    }
    /* do the conversion */
    dst = opencc_character_conversion(&src, dst_type);
    if ((dst == NULL) && (dst->buf == NULL))
    {
        os_kprintf("Change character failed.\n");
        return -1;
    }

    os_kprintf("size %d.\n", dst->buf_size);

    free(dst->buf);
    free(dst);
    
    return 0;
}

#ifdef OS_USING_SHELL
#include <shell.h>
SH_CMD_EXPORT(cc_test_cmd, cc_test, "character conversion test function");
#endif /* OS_USING_SHELL */

